package vek.GUI.MapView;

import vek.GUI.*;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.*;

/**
 * The part of the GUI containing MarkingsView (the icons with the POI's) and the background, allowing the user to scroll and see different parts of the two.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 * 
 */

public class ScrollableView extends JScrollPane{
    
        private LayerHandler jlayeredPane; // compoent which handles the order which component are shown, higher priority components are shown topmost
        private Dimension sizeOfComponents;
        private Dimension screenSize;
        private boolean isVisible = true;
        
 /**
  * Creates the ScrollableView and adds its content (
  * @param size the preffered size of this component
  * @param compSize the size of the component which this component contains
  * @param lh the layer handler which handles drawing components on top of each other
  * @param scrSize the screen size of the terminal
  */
    public ScrollableView(Dimension size, Dimension compSize, 
            LayerHandler lh, Dimension scrSize)
    {
        //object creation
        sizeOfComponents = compSize;
        jlayeredPane = lh;
        screenSize = scrSize;

        
        //variable change
        setPreferredSize(size);
        
        getVerticalScrollBar().setUnitIncrement( (int) (compSize.getHeight() / 15) );
        getHorizontalScrollBar().setUnitIncrement( (int) (compSize.getWidth() / 15) );
        System.out.println( getVerticalScrollBar().getPreferredSize() );
        getVerticalScrollBar().setPreferredSize(new Dimension((int)(screenSize.getWidth() / 40), (int) (screenSize.getHeight() / 40) ));
        getHorizontalScrollBar().setPreferredSize(new Dimension((int)(screenSize.getHeight() / 40), (int) (screenSize.getWidth() / 40) ));
        
        addContent();
    }
    
     /**
     * Centers the screen on a specific point on the map.
     */
    /**
     * Snaps the view of to the center of the point which represents the location of the MarkingButton.
     * This method is run when a MarkingButton is clicked, and the view of this ScrollPane centers on the button which is clicked. 
     * @param p the location to snap to
     */
    
    public void zapTo(Point p)
    {
        double[] halfOfScreen = getSizeOfHalfOfView();
        int posX = (int) (p.getX() - halfOfScreen[0]);
        int posY = (int) (p.getY() - halfOfScreen[1]);
        
        if( posX < 0)
        {
            posX = 0;
        }
        if ( posY < 0)
        {
            posY = 0;
        } 
        Point zapToPoint = new Point(posX, posY);
        getViewport().setViewPosition(zapToPoint);
    }
    /**
     * Creates an array containing half the width and height of itself (ScrollableView). Width is at 0 and height at 1.
     * @return double Half the size of the itself (ScrollableView).
     */
    /**
     * Gets half the size of tis component.
     * @return a double array containg the half the size of this component (x first, then y)
     */
    
    private double[] getSizeOfHalfOfView()
    {
        double[] returnedDouble = new double[2];
        returnedDouble[0] = (getSize().getWidth() / 2);
        returnedDouble[1] = (getSize().getHeight() / 2);
        return returnedDouble;
    }
    
    /**
     * Adds the layerhandler to this component
     */
    
    private void addContent()
    {
         getViewport().add( jlayeredPane);
    }

}
